package com.banshuibao.bsb_android.download;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.activity.BaseActivity;
import com.banshuibao.bsb_android.fragment.ClassListFragment;
import com.banshuibao.bsb_android.fragment.MediaTabDownloadFragment;
import com.banshuibao.bsb_android.fragment.RadioTabDownloadFragment;

/**
 * 下载列表界面
 *
 * @author CC视频
 */
public class DownloadListActivity extends BaseActivity {

    private FragmentManager manager;

    private RadioGroup radioGroup;

    private MediaTabDownloadFragment mediaTabDownloadFragment;

    private RadioTabDownloadFragment radioTabDownloadFragment;

    private int type;

    private String tag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_list);
        tag = getIntent().getStringExtra("tag");
        initView();
    }

    private void initView() {
        radioGroup = findViewById(R.id.rg);
        manager = getSupportFragmentManager();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == type)
                    return;
                hideFragment(mediaTabDownloadFragment, radioTabDownloadFragment);
                FragmentTransaction transaction = manager.beginTransaction();
                switch (i) {
                    case R.id.title1:
                        if (mediaTabDownloadFragment == null) {
                            mediaTabDownloadFragment = new MediaTabDownloadFragment();
                            transaction.add(R.id.fragment, mediaTabDownloadFragment);
                        } else
                            transaction.show(mediaTabDownloadFragment);
                        break;
                    case R.id.title2:
                        if (radioTabDownloadFragment == null) {
                            radioTabDownloadFragment = new RadioTabDownloadFragment();
                            transaction.add(R.id.fragment, radioTabDownloadFragment);
                        } else
                            transaction.show(radioTabDownloadFragment);
                        break;
                }
                transaction.commit();
                type = i;
            }
        });
        radioGroup.check(R.id.title1);
    }

    private void hideFragment(Fragment... fragments) {
        for (Fragment fragment : fragments) {
            if (fragment != null)
                manager.beginTransaction().hide(fragment).commit();
        }
    }
}
